package pl.edu.agh.gronoMining.owlToGraphConverter.visualiser;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jgraph.JGraph;
import org.jgrapht.ext.JGraphModelAdapter;

import pl.edu.agh.gronoMining.owlToGraphConverter.graph.Graph;

public class GraphVisualiser implements ActionListener {
	
	private JFrame frame;
	private JComboBox comboBox;
	private JPanel centerPanel;

	public GraphVisualiser(String name) {
		frame = new JFrame(name);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(600, 600));
		frame.getContentPane().setLayout(new BorderLayout());
	}
	
	public void show(Graph[] graphs) {

		JPanel topPanel = new JPanel(new FlowLayout());
		topPanel.add(new JLabel("Avalible graphs:"));
		List<Graph> avalibleGraphsList = new ArrayList<Graph>();
		int graphId = 1;
		for (Graph g : graphs) {
			g.setId(graphId);
			avalibleGraphsList.add(g);
			graphId++;
		}
		comboBox = new JComboBox(avalibleGraphsList.toArray());
		comboBox.addActionListener(this);
		topPanel.add(comboBox);		
		
		centerPanel = new JPanel(new BorderLayout());
		
		frame.getContentPane().add(topPanel, BorderLayout.PAGE_START);
		frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
		
		if (graphs.length > 0) {
			JGraphModelAdapter m_jgAdapter = new JGraphModelAdapter(graphs[0].getSimpleGraph());
			JGraph jgraph = new JGraph(m_jgAdapter);
			centerPanel.add(jgraph, BorderLayout.CENTER);
		}
		
		frame.pack();
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		
		Graph g = (Graph) comboBox.getSelectedItem();
		System.out.println(g);
		
		centerPanel.removeAll();
		
		JGraphModelAdapter m_jgAdapter = new JGraphModelAdapter(g.getSimpleGraph());
		JGraph jgraph = new JGraph(m_jgAdapter);
		centerPanel.add(jgraph, BorderLayout.CENTER);
		
		frame.pack();
	}
	
}
